package vp.remedies.dr.home.fragment;

import java.util.ArrayList;
import java.util.List;

import vp.remedies.dr.home.R;
import vp.remedies.dr.home.activity.TypesofDiseasesActivity;
import vp.remedies.dr.home.db.AndroidSQLiteHelper;
import vp.remedies.dr.home.so.TypesofDiseasesList;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

@SuppressLint({ "ValidFragment", "NewApi" })
public class TypesOfDiseasesMetro1Fragment extends Fragment {
	public static List<TypesofDiseasesList> typesOfDiseaseList;

	public TypesOfDiseasesMetro1Fragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.types_of_diseases_metro_1,
				container, false);

		List<TypesofDiseasesList> typesOfDiseaseList = getTypesofDiseasesList();

		LinearLayout linearLayoutType1 = (LinearLayout) rootView
				.findViewById(R.id.type1);
		LinearLayout linearLayoutType2 = (LinearLayout) rootView
				.findViewById(R.id.type2);
		LinearLayout linearLayoutType3 = (LinearLayout) rootView
				.findViewById(R.id.type3);
		LinearLayout linearLayoutType4 = (LinearLayout) rootView
				.findViewById(R.id.type4);
		LinearLayout linearLayoutType5 = (LinearLayout) rootView
				.findViewById(R.id.type5);
		LinearLayout linearLayoutType6 = (LinearLayout) rootView
				.findViewById(R.id.type6);
		LinearLayout linearLayoutType7 = (LinearLayout) rootView
				.findViewById(R.id.type7);
		LinearLayout linearLayoutType8 = (LinearLayout) rootView
				.findViewById(R.id.type8);
		LinearLayout linearLayoutType9 = (LinearLayout) rootView
				.findViewById(R.id.type9);
		LinearLayout linearLayoutType10 = (LinearLayout) rootView
				.findViewById(R.id.type10);
		LinearLayout linearLayoutTypeMoreTypes = (LinearLayout) rootView
				.findViewById(R.id.moretypes);

		final int type1 = ((TypesofDiseasesList) typesOfDiseaseList.get(0))
				.getId();
		final String typeName1 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(0)).getName();
		int id1 = getResources().getIdentifier("i" + type1, "drawable",
				getActivity().getPackageName());

		linearLayoutType1.setBackgroundResource(id1);
		linearLayoutType1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type1);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type2 = ((TypesofDiseasesList) typesOfDiseaseList.get(1))
				.getId();
		final String typeName2 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(1)).getName();
		int id2 = getResources().getIdentifier("i" + type2, "drawable",
				getActivity().getPackageName());
		linearLayoutType2.setBackgroundResource(id2);
		linearLayoutType2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type2);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type3 = ((TypesofDiseasesList) typesOfDiseaseList.get(2))
				.getId();
		String typeName3 = ((TypesofDiseasesList) typesOfDiseaseList.get(2))
				.getName();
		int id3 = getResources().getIdentifier("i" + type3, "drawable",
				getActivity().getPackageName());
		linearLayoutType3.setBackgroundResource(id3);
		linearLayoutType3.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type3);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type4 = ((TypesofDiseasesList) typesOfDiseaseList.get(3))
				.getId();
		final String typeName4 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(3)).getName();
		int id4 = getResources().getIdentifier("i" + type4, "drawable",
				getActivity().getPackageName());
		linearLayoutType4.setBackgroundResource(id4);
		linearLayoutType4.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type4);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type5 = ((TypesofDiseasesList) typesOfDiseaseList.get(4))
				.getId();
		final String typeName5 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(4)).getName();
		int id5 = getResources().getIdentifier("i" + type5, "drawable",
				getActivity().getPackageName());
		linearLayoutType5.setBackgroundResource(id5);
		linearLayoutType5.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type5);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type6 = ((TypesofDiseasesList) typesOfDiseaseList.get(5))
				.getId();
		final String typeName6 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(5)).getName();
		int id6 = getResources().getIdentifier("i" + type6, "drawable",
				getActivity().getPackageName());
		linearLayoutType6.setBackgroundResource(id6);
		linearLayoutType6.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type6);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type7 = ((TypesofDiseasesList) typesOfDiseaseList.get(6))
				.getId();
		final String typeName7 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(6)).getName();
		int id7 = getResources().getIdentifier("i" + type7, "drawable",
				getActivity().getPackageName());
		linearLayoutType7.setBackgroundResource(id7);
		linearLayoutType7.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type7);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type8 = ((TypesofDiseasesList) typesOfDiseaseList.get(7))
				.getId();
		final String typeName8 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(7)).getName();
		int id8 = getResources().getIdentifier("i" + type8, "drawable",
				getActivity().getPackageName());
		linearLayoutType8.setBackgroundResource(id8);
		linearLayoutType8.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type8);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type9 = ((TypesofDiseasesList) typesOfDiseaseList.get(8))
				.getId();
		final String typeName9 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(8)).getName();
		int id9 = getResources().getIdentifier("i" + type9, "drawable",
				getActivity().getPackageName());

		linearLayoutType9.setBackgroundResource(id9);
		linearLayoutType9.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type9);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type10 = ((TypesofDiseasesList) typesOfDiseaseList.get(9))
				.getId();
		final String typeName10 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(9)).getName();
		int id10 = getResources().getIdentifier("i" + type10, "drawable",
				getActivity().getPackageName());

		linearLayoutType10.setBackgroundResource(id10);
		linearLayoutType10.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type10);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		linearLayoutTypeMoreTypes
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Toast.makeText(getActivity(), "Jai Sai Rama",
								Toast.LENGTH_LONG).show();
						Fragment fragment = new TypesOfDiseasesMetro2Fragment();
						FragmentManager fragmentManager = getFragmentManager();
						fragmentManager
								.beginTransaction()
								.replace(R.id.frame_container, fragment,
										"TypesOfDiseasesMetro2Fragment")
								.commit();
					}
				});

		return rootView;
	}

	public static List<TypesofDiseasesList> getTypesofDiseasesList() {
		typesOfDiseaseList = new ArrayList<TypesofDiseasesList>();
		AndroidSQLiteHelper helper = AndroidSQLiteHelper.getInstance();
		/*
		 * Cursor cursor = helper .getCursorRaw(
		 * "SELECT  id , name FROM types_of_diseases_en where ORDERED BY name ASC"
		 * , null);
		 */
		String orderBy = "name" + " ASC";
		Cursor cursor = helper.getReadableDatabase().query(
				"types_of_diseases_en", new String[] { "id", "name" }, null,
				null, null, null, orderBy);

		while (cursor.moveToNext()) {
			TypesofDiseasesList diseasesList = new TypesofDiseasesList();
			int id = cursor.getInt(0);
			String name = cursor.getString(1);
			diseasesList.setId(id);
			diseasesList.setName(name);
			typesOfDiseaseList.add(diseasesList);
		}
		return typesOfDiseaseList;
	}

}
